<template>
  
  <a-card :bordered="false">
    <a-spin :spinning="spinning" :delay="delayTime">
          <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="6">
            <a-form-item label="据点">
              <a-select allowClear @select="checkInatsite" style="width: 100px" placeholder="据点" v-model="queryParam.inatsite">
                <a-select-option  v-for="(item, index) in inatsites" :key="index" :value="item.value">
                  {{ item.value }}
                </a-select-option>
              </a-select>
              <span style="margin-left:20px">
                {{queryParam.inatsiteName}}
              </span>
              <!-- <a-input placeholder="请输入据点（cmt_xcbzId）" v-model="queryParam.inatsite"></a-input> -->
            </a-form-item>
            
          </a-col>
          <a-col :span="4">
            <a-form-item label="HUB仓代码">
              <a-select allowClear style="width: 150px" placeholder="选择HUB仓代码" v-model="queryParam.inat004" >
                <a-select-option  v-for="(item, index) in inat004s" :key="index" :value="item.value">
                  {{ item.value }}
                </a-select-option>
              </a-select>
              <!-- <a-input placeholder="请输入HUB仓代码（cmt_xcbz006）" v-model="queryParam.inat004"></a-input> -->
            </a-form-item>
          </a-col>
          
            <a-col :span="3">
              <a-form-item label="年度">
                <a-input allowClear style="width: 140px" placeholder="请输入年度" v-model="queryParam.inat008"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="期别">
                <a-input allowClear style="width: 150px" placeholder="请输入期别" v-model="queryParam.inat009"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
            </a-col> 
          <!-- <a-col>
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col> -->
        </a-row>
        <a-row :gutter="24">    
          <a-col :span="4">
              <a-form-item label="料件编号">
                <a-input allowClear style="width: 140px" placeholder="请输入件编号" v-model="queryParam.inat001"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="库存管理特征">
                <a-input allowClear style="width: 150px" placeholder="请输入库存管理特征" v-model="queryParam.inat003"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="批号">
                <a-input allowClear style="width: 150px" placeholder="请输入批号" v-model="queryParam.inat006"></a-input>
              </a-form-item>
            </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXlsHub('HUB仓数据')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
      
      <!-- 环境准备 -->
      <a-button icon="down-square" @click="environmentPreparation()">环境准备</a-button>
      <!-- 导入Hub仓库 -->
      <a-button icon="import" @click="importHubWarehouse()">导入Hub仓库</a-button>
      <!-- 导入客户仓库 -->
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl1" @change="handleImportExcel1">
        <a-button icon="import">导入客户仓库</a-button>
      </a-upload>

      <!-- <a-button icon="import" @click="importCustomerWarehouse()">导入客户仓库</a-button> -->
      <!-- 数据匹配 -->
      <a-button icon="diff" @click="dataMatching()">数据匹配</a-button>
      <!-- 数据导出 -->
      <a-button icon="download" @click="handleExportXlsHub('HUB仓数据')">数据导出</a-button>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <ch-hub-modal ref="modalForm" @ok="modalFormOk"></ch-hub-modal>
    </a-spin>

  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice} from '@/utils/mixin'
  import { deleteAction, getAction, putAction, postAction, httpAction, downFile, getFileAccessHttpUrl } from '@/api/manage'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ChHubModal from './modules/ChHubModal'

  export default {
    name: 'ChHubList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      ChHubModal
    },
    data () {
      return {
        spinning: false,
        delayTime: 500,
        queryParam:{
          inatsite:'CH',
          inat004:'B11',
          inatsiteName:'深圳市昌红科技股份有限公司',
        },
        
        description: 'HUB仓数据管理页面',
        // 据点
        inatsites:[],
        // hub仓代码
        inat004s:[],
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'据点（cmt_xcbzId）',
            align:"center",
            dataIndex: 'inatsite'
          },
          {
            title:'料件编号（cmt_xcbz003）',
            align:"center",
            dataIndex: 'inat001'
          },
          {
            title:'库存管理特征（cmt_xcbz005）',
            align:"center",
            dataIndex: 'inat003'
          },
          {
            title:'HUB仓代码（cmt_xcbz006）',
            align:"center",
            dataIndex: 'inat004'
          },
          {
            title:'批号（cmt_xcbz008）',
            align:"center",
            dataIndex: 'inat006'
          },
          {
            title:'单位（cmt_xcbz009）',
            align:"center",
            dataIndex: 'inat007'
          },
          {
            title:'年度',
            align:"center",
            dataIndex: 'inat008'
          },
          {
            title:'期别',
            align:"center",
            dataIndex: 'inat009'
          },
          {
            title:'单价',
            align:"center",
            dataIndex: 'inat010'
          },
          {
            title:'期末结存数量（cmt_xcbz901）',
            align:"center",
            dataIndex: 'inat015'
          },
          {
            title:'本期入库的数量（cmt_xcbz201）',
            align:"center",
            dataIndex: 'inat016'
          },
          {
            title:'客户数量',
            align:"center",
            dataIndex: 'customerQty'
          },
          {
            title:'当前数量',
            align:"center",
            dataIndex: 'currentQty'
          },
          {
            title:'结余数量',
            align:"center",
            dataIndex: 'remainder'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/ch/chHub/list",
          delete: "/ch/chHub/delete",
          deleteBatch: "/ch/chHub/deleteBatch",
          exportXlsUrl: "/ch/chHub/exportXls",
          importExcelUrl: "ch/chHub/importExcel",
          exportHubList:'/ch/chHub/exportHubList',
          importExcel:'/ch/chCustomerPositions/importExcel',
          dataMatching:'/ch/chHub/dataMatching',
          
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      // 加载据点
      this.selectInatsites();
      // 加载hub仓
      this.selectInat004s();
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
      importExcelUrl1: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcel}`;
      },
    },
    methods: {
      // 据点
      selectInatsites(){
        this.inatsites = [
          {
            text:'深圳市昌红科技有限公司',
            value:'CH'
          },
          {
            text:'深圳市柏明胜有限公司',
            value:'BMS'
          },
          {
            text:'浙江市柏明胜有限公司',
            value:'ZBMS'
          },
          {
            text:'上海市力因有限公司',
            value:'SHLY'
          },
          {
            text:'浙江硕昌有限公司',
            value:'ZSC'
          },
          
        ]
      },
      // hub仓代码
      selectInat004s(){
        this.inat004s = [
          {
            text:'B11',
            value:'B11'
          },
          {
            text:'B14',
            value:'B14'
          },
          {
            text:'*',
            value:'*'
          },
        ]
      },
      // 选中据点触发事件
      checkInatsite(value){
        switch (value) {
          case 'CH':
          this.queryParam.inatsiteName = '深圳市昌红科技股份有限公司'
          break;
        
          case 'BMS':
          this.queryParam.inatsiteName = '深圳市柏明胜有限公司'
          break;

          case 'ZBMS':
          this.queryParam.inatsiteName = '浙江市柏明胜有限公司'
          break;

          case 'SHLY':
          this.queryParam.inatsiteName = '上海市力因有限公司'
          break;
          case 'ZSC':
          this.queryParam.inatsiteName = '浙江硕昌有限公司'
          break;

          default:
            break;
        }
       

        console.log(value);
      },
      // // 环境准备
      environmentPreparation(){
        var params = this.queryParam
        console.log('params',params);
        this.spinning = !this.spinning;
        getAction(this.url.exportHubList, params).then((res) => {
          this.spinning = !this.spinning;
          if (res.code==200) {
            this.$message.success(res.message)
            console.log("res:", res);
          }else {
            this.$message.warning(res.message)
          }  
        })
      },
      // 导入Hub仓库
      importHubWarehouse(){
        this.searchQuery()
      },
      // 导入客户仓库
      // importCustomerWarehouse(){

      // },
      // 数据匹配
      dataMatching(){
        this.spinning = !this.spinning;
        getAction(this.url.dataMatching).then((res) => {
          this.spinning = !this.spinning;
          if (res.code==200) {
            this.$message.success(res.message)
            this.loadData();
            console.log("res:", res);
          }else {
            this.$message.warning(res.message)
          }  
        })
      },
      // 数据导出
      // dataExport(){
      //   this.handleExportXlsHub('HUB仓数据')
      // },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'inatsite',text:'据点（cmt_xcbzId）',dictCode:''})
        fieldList.push({type:'string',value:'inat001',text:'料件编号（cmt_xcbz003）',dictCode:''})
        fieldList.push({type:'string',value:'inat003',text:'库存管理特征（cmt_xcbz005）',dictCode:''})
        fieldList.push({type:'string',value:'inat004',text:'HUB仓代码（cmt_xcbz006）',dictCode:''})
        fieldList.push({type:'string',value:'inat006',text:'批号（cmt_xcbz008）',dictCode:''})
        fieldList.push({type:'string',value:'inat007',text:'单位（cmt_xcbz009）',dictCode:''})
        fieldList.push({type:'string',value:'inat008',text:'年度',dictCode:''})
        fieldList.push({type:'string',value:'inat009',text:'期别',dictCode:''})
        fieldList.push({type:'string',value:'inat010',text:'单价',dictCode:''})
        fieldList.push({type:'string',value:'inat015',text:'期末结存数量（cmt_xcbz901）',dictCode:''})
        fieldList.push({type:'string',value:'customerQty',text:'客户数量',dictCode:''})
        fieldList.push({type:'string',value:'currentQty',text:'当前数量',dictCode:''})
        fieldList.push({type:'string',value:'remainder',text:'结余数量',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
.a{
   
}
  @import '~@assets/less/common.less';
</style>